export function findLocalPathByItem(remoteItem, localItems) {
  console.log('查找匹配项，远程项:', remoteItem);
  console.log('本地项列表:', localItems);
  
  if (!Array.isArray(localItems)) {
    console.error('localItems不是数组:', localItems);
    return null;
  }
  
  // 预处理数据，移除特殊字符
  const processedRemoteItem = processItem(remoteItem);
  const processedLocalItems = localItems.map(processItem);
  
  for (let i = 0; i < processedLocalItems.length; i++) {
    const localItem = processedLocalItems[i];
    const isMatch = isItemMatch(processedRemoteItem, localItem);
    
    console.log(`比较远程项与本地项[${i}]:`, isMatch);
    if (isMatch) {
      console.log('找到匹配项，localpath:', localItem.localpath);
      return localItem.localpath;
    }
  }
  
  console.log('未找到匹配项');
  return null;
}

// 只比较两个对象共有的字段（忽略subject）
function isItemMatch(remoteItem, localItem) {
  // 本地项中存在的字段（排除localpath）
  const localFields = Object.keys(localItem).filter(key => key !== 'localpath');
  
  // 检查所有本地项字段是否与远程项匹配
  return localFields.every(field => {
    // 特殊处理字符串字段
    if (typeof remoteItem[field] === 'string' && typeof localItem[field] === 'string') {
      const cleanRemote = cleanString(remoteItem[field]);
      const cleanLocal = cleanString(localItem[field]);
      return cleanRemote === cleanLocal;
    }
    
    // 基本类型直接比较
    return remoteItem[field] === localItem[field];
  });
}

// 清理字符串，移除特殊字符并统一大小写
function cleanString(str) {
  // 移除零宽空格和其他不可见字符
  str = str.replace(/[\u200B-\u200D\uFEFF]/g, '');
  // 移除多余空格并转换为小写
  return str.trim().toLowerCase();
}

// 处理项目中的特殊字符
function processItem(item) {
  const processed = {...item};
  for (const key in processed) {
    if (typeof processed[key] === 'string') {
      processed[key] = cleanString(processed[key]);
    }
  }
  return processed;
}